package com.snap.vseries.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.snap.vseries.AppConfig;
import com.snap.vseries.R;
import com.snap.vseries.base.OnItemClickListener;
import com.snap.vseries.bean.ExpEntity;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.internal.DebouncingOnClickListener;

import static com.snap.vseries.fragment.NewExperimentTemplateFragment.CUSTOM_SETTING;

public class NewExperimentTemplateAdapter extends RecyclerView.Adapter<NewExperimentTemplateAdapter.TemplateHolder> {
    private final Context context;
    private List<ExpEntity> newExperimentTemplateList;
    private OnItemClickListener onItemClickListener;

    public NewExperimentTemplateAdapter(List<ExpEntity> newExperimentTemplateList, Context context, OnItemClickListener onItemClickListener) {
        this.newExperimentTemplateList = newExperimentTemplateList;
        this.context = context;
        this.onItemClickListener = onItemClickListener;
    }

    @NonNull
    @Override
    public TemplateHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_new_experiment_template, parent, false);
        TemplateHolder holder = new NewExperimentTemplateAdapter.TemplateHolder(view);
        bindClick(holder);
        return holder;
    }

    private void bindClick(TemplateHolder holder) {
        holder.itemView.setOnClickListener(new DebouncingOnClickListener() {
            @Override
            public void doClick(View v) {
                if (onItemClickListener != null) {
                    onItemClickListener.onItemClick(NewExperimentTemplateAdapter.this, v, holder.getLayoutPosition());
                }
            }
        });
    }

    @Override
    public void onBindViewHolder(@NonNull TemplateHolder holder, int position) {
        String testProject = newExperimentTemplateList.get(position).getTestProject();
        holder.tvTemplateName.setText(testProject);
        holder.tvTemplateDesc.setText(newExperimentTemplateList.get(position).getDescription());
        if (!AppConfig.Dynamic.custom_setting) {
            holder.img.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.icon_card_temple));
            return;
        }
        if (CUSTOM_SETTING.equals(testProject)) {
            holder.img.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.icon_custom));
        } else {
            holder.img.setImageDrawable(ContextCompat.getDrawable(context, R.drawable.icon_card_temple));
        }
    }

    @Override
    public int getItemCount() {
        return newExperimentTemplateList.size();
    }

    class TemplateHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.tv_template_name)
        TextView tvTemplateName;
        @BindView(R.id.tv_template_desc)
        TextView tvTemplateDesc;
        @BindView(R.id.img)
        ImageView img;

        TemplateHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

}
